﻿package mortal.game.view.copy.ZZXYBattle
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.task.view.*;
    import mortal.mvc.interfaces.*;

    public class ZZXYBattleMoudle extends BaseWindow
    {
        private var _label2:GImageBitmap;
        private var _bossBg:GImageBitmap;
        private var _reward:TaskReward;
        private var _rooms:Array;
        private var _titleBg:GImageBitmap;
        private var _txtTips:GTextFiled;
        private var _btnEnter:GLoadingButton;
        private var _page:PageSelecter;
        private var _selectedRoom:ZZXYBattleRoom;
        private var _timer:Timer;
        private var _roomList:Dictionary;
        private var _roomNum:int = 0;
        private const PageNum:int = 8;
        private var _firstOpen:Boolean = true;
        private var _lastClickTime:int = -1;

        public function ZZXYBattleMoudle(param1:ILayer = null)
        {
            super(param1);
            this.setSize(817, 562);
            this.title = Language.getString(20986);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            if (this._timer == null)
            {
                this._timer = new Timer(6000);
                this._timer.addEventListener(TimerEvent.TIMER, this.every2Seconds);
            }
            this._timer.start();
            return;
        }// end function

        public function updateCopyInfo(param1:TCopy) : void
        {
            var _loc_5:STaskReward = null;
            if (param1 == null)
            {
                return;
            }
            var _loc_2:* = param1.rewardShow.split("#");
            var _loc_3:Array = [];
            var _loc_4:int = 0;
            while (_loc_4 < _loc_2.length)
            {
                
                _loc_5 = new STaskReward();
                _loc_5.reward = new SReward();
                _loc_5.reward.type = EReward._ERewardItem;
                _loc_5.reward.code = int(_loc_2[_loc_4]);
                _loc_5.reward.num = -1;
                _loc_5.levelMax = 1111111;
                _loc_3.push(_loc_5);
                _loc_4++;
            }
            this._reward.updateRewards(_loc_3);
            return;
        }// end function

        public function updateRoomInfos(param1:Dictionary) : void
        {
            var _loc_2:* = undefined;
            var _loc_3:int = 0;
            if (isDisposed)
            {
                return;
            }
            this._roomNum = 0;
            for (_loc_2 in param1)
            {
                
                var _loc_6:String = this;
                var _loc_7:* = this._roomNum + 1;
                _loc_6._roomNum = _loc_7;
            }
            this._page.maxPage = Math.ceil(this._roomNum / this.PageNum);
            this._roomList = param1;
            this.rebuildRooms();
            if (this._firstOpen)
            {
                this._firstOpen = false;
                if (ZZXYBattleController.myRoomId >= 0)
                {
                    _loc_3 = Math.ceil(ZZXYBattleController.myRoomId / this.PageNum);
                    this._page.currentPage = _loc_3;
                }
                this.onPageChange();
                this.autoToSelectedRoom();
            }
            else
            {
                this.onPageChange();
            }
            return;
        }// end function

        private function autoToSelectedRoom() : void
        {
            var _loc_1:int = 0;
            if (ZZXYBattleController.myRoomId >= 0)
            {
                _loc_1 = int((ZZXYBattleController.myRoomId - 1) % this.PageNum);
                this.updateSelected(this.getRoom(_loc_1));
            }
            else if (this._selectedRoom == null && this._rooms != null && this._rooms.length > 0)
            {
                this.updateSelected(this.getRoom(0));
            }
            return;
        }// end function

        public function updateSelected(param1:ZZXYBattleRoom) : void
        {
            var _loc_2:ZZXYBattleRoom = null;
            if (this._selectedRoom == param1)
            {
                return;
            }
            for each (_loc_2 in this._rooms)
            {
                
                _loc_2.selecte(false);
            }
            this._selectedRoom = param1;
            if (this._selectedRoom != null)
            {
                this._selectedRoom.selecte(true);
            }
            return;
        }// end function

        private function every2Seconds(event:TimerEvent) : void
        {
            GameProxy.copy.zzxyBattleGetRoomList();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._firstOpen = true;
            this._rooms = [];
            this._bossBg = UIFactory.gImageBitmap(null, 9, 38, this);
            LoaderHelp.setBitmapdata(ImagesConst.ZZXYBattleModuleBg + ".swf", this._bossBg);
            this.pushUIToDisposeVec(UIFactory.bg(535, 38, 277, 522, this));
            this._label2 = UIFactory.gImageBitmap(ImagesConst.ZZXYBattle_Label2, 541, 42, this);
            this._reward = UICompomentPool.getUICompoment(TaskReward) as TaskReward;
            this._reward.x = 276;
            this._reward.y = 508;
            this.addChild(this._reward);
            this._titleBg = UIFactory.gImageBitmap(null, 538, 454, this);
            LoaderHelp.setBitmapdata(ImagesConst.ZZXYBattleTitleBg + ".swf", this._titleBg);
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.size = 15;
            this._txtTips = UIFactory.gTextField("", 555, this._titleBg.y + 9, 268, 60, this, _loc_1);
            this._txtTips.filters = [FilterConst.NpcNameFilter];
            this._txtTips.multiline = true;
            this._txtTips.wordWrap = true;
            this._txtTips.htmlText = Language.getString(20987);
            this._btnEnter = UIFactory.gLoadingButton(ImagesConst.ZZXYBattleEnterBtn_upSkin, 600, 519, 157, 39, this);
            this._page = UIFactory.pageSelecter(608, 426, this, PageSelecter.CompleteMode);
            this._page.maxPage = 1;
            this._page.configEventListener(Event.CHANGE, this.onPageChange);
            this._btnEnter.configEventListener(MouseEvent.CLICK, this.clickEnterHandler);
            this.updateCopyInfo(CopyConfig.instance.getCopyInfoByCode(GameConst.ZZXYBattleCopyId));
            this.every2Seconds(null);
            return;
        }// end function

        private function onPageChange(param1 = null) : void
        {
            var _loc_6:ZZXYBattleRoom = null;
            var _loc_7:int = 0;
            var _loc_2:* = this._page.currentPage - 1;
            var _loc_3:* = _loc_2 * this.PageNum;
            var _loc_4:* = Math.min(this._roomNum - _loc_2 * this.PageNum, this.PageNum);
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = this.getRoom(_loc_5);
                _loc_7 = _loc_3 + _loc_5 + 1;
                _loc_6.updateRoomNumber(_loc_7);
                if (this._roomList != null && this._roomList[_loc_7] != null)
                {
                    _loc_6.updatePlayerNum(int(this._roomList[_loc_7]));
                }
                if (ZZXYBattleController.myRoomId > 0)
                {
                    if (_loc_7 == ZZXYBattleController.myRoomId)
                    {
                        this._selectedRoom = _loc_6;
                        _loc_6.selecte(true);
                    }
                    else
                    {
                        _loc_6.selecte(false);
                    }
                }
                else if (_loc_7 == 1)
                {
                    this.updateSelected(_loc_6);
                }
                _loc_5++;
            }
            DisplayUtil.delNotUse(this._rooms, _loc_5);
            return;
        }// end function

        private function getRoom(param1:int) : ZZXYBattleRoom
        {
            var _loc_2:ZZXYBattleRoom = null;
            if (param1 < 0)
            {
                return null;
            }
            _loc_2 = this._rooms[param1];
            if (_loc_2 == null)
            {
                _loc_2 = UICompomentPool.getUICompoment(ZZXYBattleRoom) as ZZXYBattleRoom;
                this._rooms[param1] = _loc_2;
                _loc_2.clickCallback = this.updateSelected;
            }
            _loc_2.x = 547 + param1 % 2 * 133;
            _loc_2.y = 70 + int(param1 / 2) * 90;
            if (_loc_2.parent == null)
            {
                this.addChild(_loc_2);
            }
            return _loc_2;
        }// end function

        private function rebuildRooms() : void
        {
            var _loc_2:int = 0;
            if (this._rooms == null)
            {
                this._rooms = [];
            }
            var _loc_1:* = Math.min(this.PageNum, this._roomNum);
            if (this._rooms.length == _loc_1)
            {
                return;
            }
            if (this._rooms.length < _loc_1)
            {
                _loc_2 = 0;
                while (_loc_2 < _loc_1)
                {
                    
                    this.getRoom(_loc_2);
                    _loc_2++;
                }
            }
            else
            {
                DisplayUtil.delNotUse(this._rooms, _loc_1);
            }
            return;
        }// end function

        private function clickEnterHandler(event:MouseEvent) : void
        {
            if (ZZXYBattleController.openData != null)
            {
                if (ZZXYBattleController.openData.openDt.time > ClockManager.instance.nowDate.time)
                {
                    MsgManager.showRollTipsMsg(Language.getString(20988));
                    return;
                }
            }
            var _loc_2:* = FrameManager.flashFrame.getTime();
            if (_loc_2 - this._lastClickTime < 800)
            {
                return;
            }
            this._lastClickTime = _loc_2;
            if (this._selectedRoom == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(20989));
                return;
            }
            if (this._selectedRoom.isFull)
            {
                MsgManager.showRollTipsMsg(Language.getString(20990));
                return;
            }
            CopyUtil.copyCheck(this.enterCopyAfterCheck, this._selectedRoom.roomId);
            return;
        }// end function

        private function enterCopyAfterCheck(param1:int) : void
        {
            GameProxy.copy.zzxyBattleGetRoomList();
            GameProxy.copy.enterZZXYBattleCopy(param1);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._label2.dispose();
            this._label2 = null;
            this._bossBg.dispose();
            this._bossBg = null;
            this._reward.dispose();
            this._reward = null;
            this._titleBg.dispose();
            this._titleBg = null;
            this._txtTips.dispose();
            this._txtTips = null;
            this._btnEnter.dispose();
            this._btnEnter = null;
            this._page.dispose();
            this._page = null;
            DisplayUtil.delNotUse(this._rooms, 0);
            this._rooms = null;
            this._selectedRoom = null;
            if (this._timer != null)
            {
                this._timer.stop();
                this._timer.removeEventListener(TimerEvent.TIMER, this.every2Seconds);
                this._timer = null;
            }
            return;
        }// end function

    }
}
